package com.alen.sun.service.Impl;

import com.alen.sun.bean.PageBean;
import com.alen.sun.bean.Room;
import com.alen.sun.dao.RoomMapper;
import com.alen.sun.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RoomServiceImpl implements RoomService {
    @Autowired
    private RoomMapper roomMapper;

    /**
     * 添加房间
     * @param room
     */
    @Override
    public Boolean addRoom(Room room) {
        //添加room之前,先判断该房间号是否已存在?
        Integer count = roomMapper.selectRoom(room);
        if(count == null || count == 0){
            //不存在该房间号
            room.setRoomStatus(0);
            Boolean flag = roomMapper.addRoom(room);
            return flag;
        }else {
            return false;
        }
    }

    /**
     * 修改房间信息
     * @param room
     */
    @Override
    public Boolean updateRoom(Room room) {
        Boolean flag = roomMapper.updateRoom(room);
        return flag;
    }

    /**
     * 删除房间
     * @param room
     */
    @Override
    public Boolean deleteRoom(Room room) {
        Boolean flag = roomMapper.deleteRoom(room);
        return flag;
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //1. 设置分页参数
        //计算参数start
        Integer start = (page-1)*pageSize;
        List<Room> folkList = roomMapper.page(start, pageSize);
        //2.查询总数
        Long count = roomMapper.count();
        PageBean pageBean = new PageBean(count,folkList);
        return pageBean;
    }


    /**
     * 分页查看空闲房间
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean pageFreeRoom(Integer page, Integer pageSize) {
        //1. 设置分页参数
        //计算参数start
        Integer start = (page-1)*pageSize;
        List<Room> folkList = roomMapper.pageFreeRoom(start, pageSize);
        //2.查询总数
        Long count = roomMapper.countFreeRoom();
        PageBean pageBean = new PageBean(count,folkList);
        return pageBean;
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public Boolean deleteByIds(List<String> ids) {
        Boolean flag = roomMapper.deleteByIds(ids);
        return flag;
    }

    /**
     * 网页搜索功能
     * @param keywords
     * @return
     */
    @Override
    public List<Room> searchByKeywords(String keywords) {
        List<Room> rooms = roomMapper.searchByKeywords(keywords);
        return rooms;
    }

    /**
     * 获取空闲房间
     * @return
     */
    @Override
    public String[] getFreeRoom() {
        List<Map<String, String>> rooms = roomMapper.getFreeRoom();
        String[] room_numbers = new String[rooms.size()];
        for (int i = 0; i < rooms.size(); i++) {
            Map<String, String> room = rooms.get(i);
            String roomNumber = room.get("room_num");
            room_numbers[i] = roomNumber;
        }
        return room_numbers;
    }

    //根据房间状态查找房间--查找空闲房间
}
